#!/usr/bin/python

#extract scores out of outdock_file


import sys, string

file_name = sys.argv[1]


out_file = open (sys.argv[2], 'w') 

#----------------------
def score_sort(a,b):
   if a[1] < b[1] :
     return -1
   elif a[1] == b[1]:
     return 0
   else:
     return 1

     


dict = {}

total_list = []

print '=========> open ' + file_name + ' <==============='
read_file = open(file_name, 'r')
for i in read_file.readlines():
#    print i[0:6]
  if (i[0:6] == '     E') and (string.strip(i[72:79]) <> 'Total'):
    name = i[7:16]
    score = float( string.strip( i[71:79] ) )
    vdw   = float( string.strip( i[37:48] ) )
    ele   = float( string.strip( i[28:35] ) )
    polsol= float( string.strip( i[50:57] ) )
    apolsol= float( string.strip( i[59:67] ) )
    if not dict.has_key(name):
      dict[name] = [score, vdw,ele,polsol,apolsol]
	#print 'test'
    elif dict[name][0] > score:
	dict[name] = [score, vdw,ele,polsol,apolsol]
	
read_file.close()
  
content = []

#print dict.keys()

items = dict.items()
items.sort(score_sort)
counter = 1
for i in items:
  dict[i[0]].append(counter)
  counter = counter + 1
  
  
content = dict.keys()

content.sort()

out_file.write('id' + '\t' + 'rank'  + '\t' + 'total' + '\t' + 'elec' + '\t' + 'vdw' + '\t' + 'polsol' + '\t' + 'apolsol' + '\n')

for i in content:
  out_file.write(i + '\t' + str(dict[i][5])  + '\t' + '%.3f'%dict[i][0] + '\t' + '%.3f'%dict[i][2] + '\t' + '%.3f'%dict[i][1] + '\t' + '%.3f'%dict[i][3] + '\t' + '%.3f'%dict[i][4] + '\n')
out_file.close()




